const app = getApp()
Page({
  data: {
    avatar:'/img/logo.png',
    nickname:'未授权',
    openid:'',
    is_question:0,
    is_interview:1
  },
  onLoad: function (options) {
    var that = this;
    if(wx.getStorageSync('userinfo').avatar == null){
      that.setData({
        avatar:'/img/logo.png'
      })
    }else{
      that.setData({
        avatar:wx.getStorageSync('userinfo').avatar
      })
    }
    if(wx.getStorageSync('userinfo').nickname == null){
      that.setData({
        nickname:'未授权'
      })
    }else{
      that.setData({
        nickname:wx.getStorageSync('userinfo').nickname
      })
    }
    this.setData({
      openid:wx.getStorageSync('openid')
    })
  },

  onShow: function () {
    var that = this;

    if(!(wx.getStorageSync('isLogin'))){
    //   wx.redirectTo({
    //     url: '/pages/center/login',
    //   })
    }

    const version = wx.getSystemInfoSync().SDKVersion;
    const compare_version = that.compareVersion(version, '2.27.1');
    if(compare_version >= 0){
      that.setData({
        is_submit_btn : 1
      })
    }else{
      that.setData({
        is_submit_btn : 0
      })
    }
    that.getApplyInfo();
  },

  //获取专家申请情况
  getApplyInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.domain + 'index/getApplyInfo/',
      method: "POST",
      data: {
        username: wx.getStorageSync('userinfo').username
      },
      header: {
        "Content-Type" : "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.setData({
          is_question:res.data.data.is_question,
          is_interview:res.data.data.is_interview
        })
      }
    })
  },

  go_other:function(e){
    wx.showToast({
      title: '此模块正在开发中...',
      icon:'none',
      duration:1000
    })
  },

  go_my_info:function(e){
    wx.navigateTo({
      url: '/pages/center/info/index',
    })
  },

  go_edit_userinfo: function (e) {
    wx.navigateTo({
      url: '/pages/center/userinfo',
    })
  },

  go_question: function (e) {
    var that = this;
    console.log(that.data.is_question);
    if(that.data.is_question == 0){
      wx.showToast({
        title: "出题专家正在审核中",
        icon: 'none',
        duration: 2000
      })
    }else if(that.data.is_question == 1){
      wx.showToast({
        title: "出题专家模块功能正在规划中",
        icon: 'none',
        duration: 2000
      })
    }else{
      wx.navigateTo({
        url: '/pages/center/info/question',
      })
    }
  },

  go_interview: function (e) {
    var that = this;
    if(that.data.is_interview == 0){
      wx.showToast({
        title: "面试专家正在审核中",
        icon: 'none',
        duration: 2000
      })
    }else if(that.data.is_interview == 1){
      wx.showToast({
        title: "面试专家模块正在规划中",
        icon: 'none',
        duration: 2000
      })
    }else{
      wx.navigateTo({
        url: '/pages/center/info/interview',
      })
    }
  },

  login_out:function(){
    wx.showModal({
      title: '',
      content: '是否确认退出',
      success(res) {
        if (res.confirm) {
          wx.removeStorageSync('isLogin');
          wx.removeStorageSync('userinfo');
          wx.redirectTo({
            url: '/pages/center/login',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    }) 
  },

  compareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)
    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }
    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])
      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }
  
    return 0
  }
  
})